package com.ssb.admin.modules.sys.role.controller;


import com.ssb.admin.common.controller.AbstractController;
import com.ssb.admin.common.utils.R;
import com.ssb.admin.common.utils.TreeNode;
import com.ssb.admin.modules.sys.permission.service.ISysPermissionService;
import com.ssb.admin.modules.sys.role.controller.dto.RoleQuery;
import com.ssb.admin.modules.sys.role.entity.SysRole;
import com.ssb.admin.modules.sys.role.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author duaichao
 * @since 2018-08-23
 */
@RestController
@Api(value = "角色管理", tags = "角色管理")
public class SysRoleController extends AbstractController {
    private final ISysPermissionService sysPermissionService;
    private final ISysRoleService sysRoleService;

    @Autowired
    public SysRoleController(ISysPermissionService sysPermissionService, ISysRoleService sysRoleService) {
        this.sysPermissionService = sysPermissionService;
        this.sysRoleService = sysRoleService;
    }
    @ApiOperation(value = "根据角色ID获取角色menus", notes = "根据角色ID获取角色menus")
    @ApiImplicitParam(name="id", value="角色ID", required = true, dataType = "long")
    @GetMapping("/role/{id}/menu")
    public R getMenus(@PathVariable Long id){
        List<TreeNode> tree = sysPermissionService.queryMenusByRoleId(id);
        return R.ok().data(tree);
    }


    @PostMapping("/role/list")
    @ApiOperation(value = "获取角色列表", notes = "用于角色管理列表")
    public R getList(@RequestBody RoleQuery dto){
        return sysRoleService.queryPage(dto);
    }

    @PostMapping("/role/create")
    @ApiImplicitParam(name="sysRole", value="角色对象实例", required = true, dataType = "SysRole")
    @ApiOperation(value = "创建角色", notes = "创建角色")
    public R create(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return R.ok();
    }
    @PostMapping("/role/update")
    @ApiImplicitParam(name="sysRole", value="角色对象实例", required = true, dataType = "SysRole")
    @ApiOperation(value = "更新角色", notes = "更新角色")
    public R update(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return R.ok();
    }
    @PostMapping("/role/delete")
    @ApiImplicitParam(name="sysRole", value="角色对象实例", required = true, dataType = "SysRole")
    @ApiOperation(value = "删除角色", notes = "删除角色")
    public R del(@RequestBody SysRole sysRole){
        sysRoleService.del(sysRole);
        return R.ok();
    }
}

